﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hidistro.Core;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Hidistro.Core.Entities;



namespace CustomMade
{

    public class CustomMadeDao
    {
        private static Database database;
        static CustomMadeDao()
        {
            database = DatabaseFactory.CreateDatabase();
        }
        //update products Straightdown
        public  void Products_Update(int productId, bool Straightdown)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("update Hishop_Products set ");
            strSql.AppendFormat("Straightdown='{0}'", Straightdown);
            strSql.AppendFormat(" where productId={0} ", productId);    

            DbCommand cmd = database.GetSqlStringCommand(strSql.ToString());
            database.ExecuteNonQuery(cmd);
        }

         public  DataTable GetStaightDownInfo(int pictureId)
        {

            DataTable table = null;
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select Straightdown from Hishop_Products");
            strSql.AppendFormat("");
            strSql.AppendFormat(" where ProductId={0}", pictureId);
            DbCommand dbcmd = database.GetSqlStringCommand(strSql.ToString());
            using (IDataReader reader = database.ExecuteReader(dbcmd))
            {
                table = DataHelper.ConverDataReaderToDataTable(reader);
            }
            return table;
        }

       
         //推荐商品
         public  DataTable CustomRole_GetUserProductStocks(int userId, int pageIndex, int pageSize, string proName)
         {
             DataTable table = null;
             StringBuilder strSql = new StringBuilder();
             strSql.AppendFormat("select top {0} s.productid,productname,ThumbnailUrl100 from hishop_stationskus t left join hishop_skus s on t.skuid=s.skuid  ", pageSize);
             strSql.AppendFormat("  left join hishop_products p on s.productid=p.productid where t.userid=@userid ");

             if (!string.IsNullOrEmpty(proName))
                 strSql.AppendFormat(" and productname like @proName ");
             if (pageIndex > 0)
             {
                 strSql.AppendFormat(" and s.productid>(select max(productid) from ( select top {0} s.productid from hishop_stationskus t left join hishop_skus s on t.skuid=s.skuid   ", pageSize * pageIndex);
                 strSql.AppendFormat(" left join hishop_products p on s.productid=p.productid where t.userid=@userid  ");

                 if (!string.IsNullOrEmpty(proName))
                     strSql.AppendFormat(" and productname like @proName ");

                 strSql.AppendFormat(" group by s.productid order by s.productid asc) pro) ");
             }
             strSql.AppendFormat(" group by s.productid,productname,ThumbnailUrl100 order by s.productid asc ");
             DbCommand dbCmd = database.GetSqlStringCommand(strSql.ToString());
             database.AddInParameter(dbCmd, "userid", DbType.Int32, userId);

             if (!string.IsNullOrEmpty(proName))
                 database.AddInParameter(dbCmd, "proName", DbType.String, proName);
             using (IDataReader reader = database.ExecuteReader(dbCmd))
             {
                 table = DataHelper.ConverDataReaderToDataTable(reader);
             }
             return table;
         
         }


         public DataTable GetInfoProdectsIsfreeShipping(int categoryId)
         { 
  
             DataTable table = null;
             StringBuilder strSql = new StringBuilder();
             strSql.AppendFormat("select IsfreeShipping from Hishop_Products");
             strSql.AppendFormat("");
             strSql.AppendFormat(" where CategoryId={0}", categoryId);
             DbCommand dbcmd = database.GetSqlStringCommand(strSql.ToString());
             using (IDataReader reader = database.ExecuteReader(dbcmd))
             {
                 table = DataHelper.ConverDataReaderToDataTable(reader);
             }
             return table;
         }



    }
}
